package com.harvest.scientific.ladder.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;

import com.harvest.scientific.ladder.R;


public class ClearEditText extends AppCompatEditText {

    // X image
    protected Drawable imgX;
    protected Boolean showRight;
    private OnClickListener onDelClickListener;

    public ClearEditText(Context context) {
        super(context);

        init();
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init();
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClearEditText);
        Drawable close = typedArray.getDrawable(R.styleable.ClearEditText_close);
        showRight = typedArray.getBoolean(R.styleable.ClearEditText_showRight, true);
        //right img
        if (showRight) {
            if (close != null) {
                imgX = close;
            } else {
                imgX = ContextCompat.getDrawable(getContext(), R.mipmap.ic_input_close);
            }
        }

        init();
    }

    void init() {
        setPadding(getPaddingLeft(),getPaddingTop(),(getPaddingRight()==0)? dip2px(getContext(), 10f) : getPaddingRight(),getPaddingBottom());
        setCompoundDrawablePadding(dip2px(getContext(), 5));
        // Set bounds of our X button
        if (showRight && imgX != null) {
            imgX.setBounds(0,
                    0,
                    imgX.getIntrinsicWidth(),
                    imgX.getIntrinsicHeight());

            manageClearButton();

            this.setOnTouchListener((v, event) -> {

                ClearEditText et = ClearEditText.this;

                // Is there an X showing?
                if (et.getCompoundDrawables()[2] == null) {
                    return false;
                }
                // Only do this for up touches
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                // Is touch on our clear button?
                if (event.getX() > et.getWidth() - et.getPaddingRight()
                        - imgX.getIntrinsicWidth() ) {
                    et.setText("");
                    if(onDelClickListener!=null){
                        onDelClickListener.onClick(this);
                    }
                    ClearEditText.this.removeClearButton();
                }
                return false;
            });

            this.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                                          int count) {

                    ClearEditText.this.manageClearButton();
                }

                @Override
                public void afterTextChanged(Editable arg0) {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                }
            });
        }
    }

    public void manageClearButton() {
        if (TextUtils.isEmpty(getText().toString())) {
            removeClearButton();
        } else {
            addClearButton();
        }
    }

    void addClearButton() {
        this.setCompoundDrawables(this.getCompoundDrawables()[0],
                this.getCompoundDrawables()[1], imgX,
                this.getCompoundDrawables()[3]);
    }

    public void removeClearButton() {
        this.setCompoundDrawables(this.getCompoundDrawables()[0],
                this.getCompoundDrawables()[1], null,
                this.getCompoundDrawables()[3]);
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface OnClickListener{
        void onClick(View view);
    }

    public void setOnDelClickListener(OnClickListener onDelClickListener) {
        this.onDelClickListener = onDelClickListener;
    }
}